package org.dragon.services.impls;

import jakarta.annotation.Resource;
import org.dragon.commons.Const;
import org.dragon.commons.Times;
import org.dragon.mappers.CredentialExtendMapper;
import org.dragon.models.Credential;
import org.dragon.services.CredentialService;
import org.springframework.stereotype.Service;

/**
 * Author:     Zhao Yan
 * DateTime:   2023/5/17 12:06
 */
@Service
public class CredentialServiceImpl implements CredentialService {

    @Resource
    private CredentialExtendMapper credentialExtendMapper;

    @Override
    public Credential findCredential(String id) {
        return credentialExtendMapper.selectByPrimaryKey(id);
    }

    @Override
    public Credential findCredential(String source, String sourceId) {
        return credentialExtendMapper.findBySourceAndSourceId(source, sourceId);
    }

    @Override
    public void save(Credential credential) {
        if (credential != null) {
            if (credential.getId() == null) {
                // 插入
                credential.setId(Const.ulid());
                credential.setAccountId("");
                credential.setCreateTime(Times.now());
                credentialExtendMapper.insert(credential);
            } else {
                // 更新
                credentialExtendMapper.updateByPrimaryKey(credential);
            }
        }
    }

    @Override
    public int updateAccountId(Credential credential) {
        return credentialExtendMapper.updateAccountId(credential);
    }
}
